package com.cxj.cxj_test_cloud.common.rxjava;

import android.text.TextUtils;

import com.trello.rxlifecycle3.LifecycleProvider;
import com.trello.rxlifecycle3.android.ActivityEvent;
import com.trello.rxlifecycle3.android.FragmentEvent;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.lang.reflect.Type;

import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * 网络请求公共处理部分(支持背压)
 */
public class RxHttpCommonTransformer<T> implements FlowableTransformer<T,T> {

    private LifecycleProvider mLifecycleProvider;
    private boolean mShowLoading;
    private Object mDestroy;
    private LoadDialog mLoadDialog;

    public RxHttpCommonTransformer(boolean isShowLoading, LifecycleProvider lifecycleProvider) {
        this.mShowLoading = isShowLoading;
        this.mLifecycleProvider=lifecycleProvider;

        mDestroy= ActivityEvent.DESTROY;
        if (lifecycleProvider != null) {
            Type genericSuperclass = lifecycleProvider.getClass().getGenericSuperclass();
            String typeName = genericSuperclass.toString();
            if (!TextUtils.isEmpty(typeName)){
                if (typeName.contains(BaseFragment.class.getName())){
                    mDestroy= FragmentEvent.DESTROY;
                }
            }
        }

    }

    @Override
    public Flowable<T> apply(Flowable<T> upstream) {
        upstream
                .onBackpressureLatest()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Subscription>() {
                    @Override
                    public void accept(Subscription subscription) throws Exception {
                        if (mShowLoading){
                            if ()
                                mLoadDialog = new LoadDialog();
                        }
                    }
                })
                .flatMap(new Function<T, Publisher<?>>() {
                    @Override
                    public Flowable<?> apply(T t) throws Exception {
                        //网络不可用
                        return null;
                    }
                })
                .doAfterTerminate(new Action() {
                    @Override
                    public void run() throws Exception {
                        if (mLoadDialog != null){
                            mLoadDialog.dismiss();
                        }
                    }
                })
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (mLoadDialog != null){
                            mLoadDialog.dismiss();
                        }
                    }
                })
                .doOnCancel(new Action() {
                    @Override
                    public void run() throws Exception {
                        if (mLoadDialog != null){
                            mLoadDialog.dismiss();
                        }
                    }
                });

        if (mLifecycleProvider != null){
            return upstream.compose(mLifecycleProvider.bindUntilEvent(mDestroy));
        }else {
            return upstream;
        }
    }
}
